package jui.business
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.TextEvent;
	
	import jui.business.vo.MessageIdVO;
	import jui.maps.events.JUIEvent;
	import jui.model.Config;
	
	import mx.core.FlexGlobals;
 	
	public class TextManager extends EventDispatcher implements IJUIManager
	{
		private var cnf: Config = Config.getConfig(); 

		/**
		 * Returns qualified class name used by ManagersFacrtory to produce this manager.
		 */
		public static function getQName(): String
		{
			return "jui.business.TextManager";	
		}		
		
		/** 
		 * Handles link events in message chat
		 * 
		 * Available link event types constants:
		 * 
		 * LINK_EVENT_REPLY_TO
		 * LINK_EVENT_DELETE_MESSAGE_BY_ID
		 */  
		public function parseLink(linkText: String):void 
		{
			trace("Link event handler: " + linkText ); // Link Clicked inside the textfield
			
			var linkTypeIdStart: int = linkText.indexOf( "LinkEvent" );
			var linkTypeIdEnd: int = linkText.indexOf( ":", linkTypeIdStart );
			var linkTypeId: String = linkText.substring(linkTypeIdStart, linkTypeIdEnd);
			var linkParams: String = linkText.substring(linkTypeIdEnd + 1);
			
			// get link event type:
			switch ( linkTypeId )
			{
				case cnf.LINK_EVENT_REPLY_TO:
					jQuickReply(linkParams);
					break;
				case cnf.LINK_EVENT_DELETE_MESSAGE_BY_ID:
					jQuickDelete(linkParams);
					break;
				case cnf.LINK_EVENT_READ_MESSAGE_BY_ID:
					jQuickRead(linkParams);
					break;
				case cnf.LINK_EVENT_UNSUBSRIBE_FROM_MESSAGE_BY_ID:
					jQuickUnsubscribe(linkParams);
					break;
				case cnf.LINK_EVENT_SUBSCRIBE_TO_MESSAGE_BY_ID:
					jQuickSubscribe(linkParams);
					break;
				case cnf.LINK_EVENT_OPEN_URL:
					jQuickOpenUrl(linkParams);
					break;
				default:
					trace ("WARNING: Undefined link type");
			}
		}
		
		private function jQuickUnsubscribe(params: String):void
		{
			var jEvent: JUIEvent = new JUIEvent(JUIEvent.JUI_SEND_MESSAGE);
			jEvent.text = "U #" + params;
			(FlexGlobals.topLevelApplication as JUI).systemManager.dispatchEvent(jEvent);
		}
		
		private function jQuickSubscribe(params: String):void
		{
			var jEvent: JUIEvent = new JUIEvent(JUIEvent.JUI_SEND_MESSAGE);
			jEvent.text = "S #" + params;
			(FlexGlobals.topLevelApplication as JUI).systemManager.dispatchEvent(jEvent);
		}
		
		private function jQuickOpenUrl(params: String):void
		{
			trace("jQuickOpenUrl: " + params);
			var jEvent: JUIEvent = new JUIEvent(JUIEvent.JUI_OPEN_URL);
			jEvent.text = "" + params;
			(FlexGlobals.topLevelApplication as JUI).systemManager.dispatchEvent(jEvent);
		}
		
		private function jQuickRead(params: String):void
		{
			var jEvent: JUIEvent = new JUIEvent(JUIEvent.JUI_SEND_MESSAGE);
			
			var midVO: MessageIdVO = new MessageIdVO(params);
			var ridReadSuffix: String = "+";
			if(!isNaN(parseInt(midVO.rid)))
			{
				ridReadSuffix = "/" + midVO.rid;
			}
			
			jEvent.text = "#" + midVO.mid + ridReadSuffix;
			(FlexGlobals.topLevelApplication as JUI).systemManager.dispatchEvent(jEvent);
		}
		
		private function jQuickDelete(replyParams: String):void
		{
			var jEvent: JUIEvent = new JUIEvent(JUIEvent.JUI_SEND_MESSAGE);
			jEvent.text = "D #" + replyParams;
			(FlexGlobals.topLevelApplication as JUI).systemManager.dispatchEvent(jEvent);
			// Flex 3 style: Application.application.systemManager.dispatchEvent(jEvent);
		}
		
		private function jQuickReply(replyParams: String):void
		{
			var evn: TextEvent = new TextEvent(TextEvent.TEXT_INPUT, false);
			
			// parse reply params:
			var slashIndex: int = replyParams.indexOf("/");
			
			// When replying to new mwssage, there's no "/" in the reply params
			if (slashIndex == -1)
			{
				mid = replyParams;
				evn.text = "#" + mid + " ";
			}
			else
			{
				var mid: String = replyParams.substring(0,slashIndex);  
				var rid: String = replyParams.substring(slashIndex + 1);
				evn.text = "#" + mid + "/" + rid + " ";
			}
			
			dispatchEvent(evn);
				//messageInput.setSelection( messageInput.text.length, messageInput.text.length);
				// another variant was pre-highlighted reply text like: "Your reply".length, messageInput.text.length );
		}
		
		public function TextManager(target:IEventDispatcher=null)
 		{
 			super(target);
 		}
	}
}